//
//  WSChannelRankCell.m
//  CharonTVProject
//
//  Created by karong on 2023/8/1.
//

#import "WSChannelRankCell.h"
#import "WSChannelRankItemCell.h"
#import "UIButton+ImageAlign.h"
#import "WSChannelItem.h"

@interface WSChannelRankCell ()

@property (weak, nonatomic) IBOutlet UICollectionView *listView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UIButton *moreBtn;
@property (nonatomic, strong) NSArray * dataArray;
@property (nonatomic, assign) CGSize cellSize;
@property (nonatomic, strong) WSVideo * object;
@end

@implementation WSChannelRankCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self.moreBtn setButtonImageTitleStyle:(WSButtonImageTitleStyleRight) padding:2.5];
}

- (void)setDataWithObject:(id)object cellSize:(CGSize)size {
    WSVideo * video = object;
    self.cellSize = size;
    self.object = video;
    self.dataArray = video.list;
    self.titleLabel.text = video.title;
    [self.listView reloadData];
}

- (IBAction)moreAction:(id)sender {
    if ([self.delegate respondsToSelector:@selector(channelRankCellMoreAtion:)]) {
        [self.delegate channelRankCellMoreAtion:self.object];
    }
}

#pragma mark UICollectionDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return MIN(self.dataArray.count, 4);
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    WSChannelRankItemCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"WSChannelRankItemCell" forIndexPath:indexPath];
    [cell setDataWithObject:self.dataArray[indexPath.row] index:indexPath.row];
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize size = CGSizeZero;
    size.width = self.cellSize.width;
    size.height = floor((self.cellSize.height - 20 - 39 - 18) / 4.0f);
    return size;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if ([self.delegate respondsToSelector:@selector(channelRankCellDidSelectAtion:)]) {
        [self.delegate channelRankCellDidSelectAtion:self.dataArray[indexPath.row]];
    }
}

@end
